
//
//  UIImageView+JSDownload.m
//  IJSOCproject
//
//  Created by shange on 2017/5/20.
//  Copyright © 2017年 jinshan. All rights reserved.
//

#import "UIImageView+JSDownload.h"
#import "UIImage+JSImage.h"
#import <AFNetworking/AFNetworking.h>
#import <UIImageView+WebCache.h>


@implementation UIImageView (JSDownload)

- (UIImage *)getImageFromUIGraphics:(CGRect)imageRect
{
    CGFloat imageW = imageRect.size.width;
    CGFloat imageH = imageW * imageRect.size.height / imageRect.size.width;
    UIGraphicsBeginImageContext(CGSizeMake(imageW, imageH));  // 开启上下文
    [self.image drawInRect:CGRectMake(0, 0, imageW, imageH)]; //画图
    self.image = UIGraphicsGetImageFromCurrentImageContext(); //获取图片
    UIGraphicsEndImageContext();                              //关闭上下文
    return self.image;
}

// 设置图片的各种形状
- (void)setCircleImageFromOriginImage:(NSString *)originImage placedolder:(UIImage *)placeholder shapeType:(shapeType)shapeType
{
    if (shapeType == circleType) {
        [self sd_setImageWithURL:[NSURL URLWithString:originImage] placeholderImage:placeholder options:SDWebImageCacheMemoryOnly completed:^(UIImage *_Nullable image, NSError *_Nullable error, SDImageCacheType cacheType, NSURL *_Nullable imageURL) {
            if (!image) {
                return;
            }
            self.image = [UIImage imageCircleFromOriginImage:image]; //设置一下圆形图抗锯齿
        }];
    } else if (shapeType == squareType) {
        [self sd_setImageWithURL:[NSURL URLWithString:originImage] placeholderImage:placeholder];
    } else {
        [self sd_setImageWithURL:[NSURL URLWithString:originImage] placeholderImage:placeholder];
    }
}

- (void)setOriginImageFromURL:(NSString *)originURL thumbnailImage:(NSString *)thumbnailUrl placeholder:(UIImage *)placeholder completed:(SDExternalCompletionBlock)completedBlock
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    // sd 的缓存  直接冲沙盒中获取
    // 缓存机制: 一个url 对应一个 image
    // 获取大图
    UIImage *originImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:originURL];
    if (originImage) {
        self.image = originImage;
        completedBlock(originImage, nil, 0, [NSURL URLWithString:originURL]);
    } else //未下载过
    {
        if (manager.isReachableViaWiFi) {
            [self sd_setImageWithURL:[NSURL URLWithString:originURL] placeholderImage:nil completed:completedBlock];
        } else if (manager.isReachableViaWWAN) {
            BOOL is3gOre4G = YES;
            if (is3gOre4G) // 3g/4g
            {
                [self sd_setImageWithURL:[NSURL URLWithString:originURL] placeholderImage:nil completed:completedBlock];
            } else {
                [self sd_setImageWithURL:[NSURL URLWithString:thumbnailUrl] placeholderImage:nil completed:completedBlock];
            }
        } else // 没有网络
        {
            UIImage *thumbnailImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:thumbnailUrl];
            if (thumbnailImage) {
                self.image = thumbnailImage;
                completedBlock(thumbnailImage, nil, 0, [NSURL URLWithString:thumbnailUrl]);
            } else { // 注意必须设置无网络的占位图或者清空
                self.image = placeholder;
            }
        }
    }
}


@end
